package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.DTO.GuahaoDTO;
import team.yidong.fenda.DTO.GuahaoMasterDTO;
import team.yidong.fenda.DTO.SysGuahaoDTO;
import team.yidong.fenda.entity.Guahao;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalDepartment;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.repository.GuaHaoRepository;
import team.yidong.fenda.repository.HospitalDepartmentRepository;
import team.yidong.fenda.repository.HospitalRepository;
import team.yidong.fenda.repository.MasterRepository;
import team.yidong.fenda.service.GuahaoService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xuanlong on 2018/3/17.
 */
@Service
public class GuahaoServiceImpl implements GuahaoService {
    @Autowired
    GuaHaoRepository guaHaoRepository;
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    MasterRepository masterRepository;
    @Autowired
    HospitalDepartmentRepository hospitalDepartmentRepository;

    @Override
    public JSONObject findGuahaoNum(Integer hospitalId, Integer hospitalDepartmentId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        List<Integer> numList = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, +i);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            String date1 = sdf.format(calendar.getTime());
            calendar.set(Calendar.HOUR_OF_DAY, 24);
            String date2 = sdf.format(calendar.getTime());
            System.out.println(date1 + "******" + date2);
            Integer sum = guaHaoRepository.findGuahaoNum(hospitalDepartmentId, date1, date2);
            System.out.println("***********" + sum);
            numList.add(sum);
        }
        GuahaoDTO guahaoDTO = new GuahaoDTO();
        guahaoDTO.setGuahaoNum(numList);
        guahaoDTO.setWeekendWorkStatus(hospitalRepository.findOne(hospitalId).getWeekendWorkStatus());
        return JSONObject.fromObject(guahaoDTO);
    }

    @Override
    public JSONArray findGuahaoMaster(String guahaoDate, Integer hospitalId, Integer departmentId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf1.parse(guahaoDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date date1 = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 24);
            Date date2 = calendar.getTime();
            String strDate1 = sdf.format(date1);
            String strDate2 = sdf.format(date2);
            List<Master> masterList = masterRepository.findByHospitalId(hospitalId);
            List<Integer> masterIdList = new ArrayList<>();
            for (Master master : masterList) {
                masterIdList.add(master.getId());
            }
            List<Guahao> guahaoList = guaHaoRepository.findGuahaoMaster(strDate1, strDate2, masterIdList, departmentId);
            Set<Master> guahaoMasterList = new HashSet<>();

            for (Guahao guahao : guahaoList) {
                Master master = masterRepository.findOne(guahao.getMasterId());
                guahaoMasterList.add(master);
            }
        /*    calendar.setTime(date);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            Date date11 = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            Date date22 = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 24);
            Date date33 = calendar.getTime();
            String strDate11 = sdf.format(date11);
            String strDate22 = sdf.format(date22);
            String strDate33 = sdf.format(date33);
            List<GuahaoMasterDTO> guahaoMasterDTOList = new ArrayList<>();
            for (Guahao guahao : guahaoList) {
                Integer masterAmNum = guaHaoRepository.sumMasterGuahaoOneDayNum(guahao.getMasterId(), strDate11, strDate22);
                Integer masterPmNum = guaHaoRepository.sumMasterGuahaoOneDayNum(guahao.getMasterId(), strDate11, strDate33);
                Master master = masterRepository.findOne(guahao.getMasterId());
                GuahaoMasterDTO guahaoMasterDTO = new GuahaoMasterDTO();
                guahaoMasterDTO.setName(master.getName());
                guahaoMasterDTO.setInfo(master.getInfo());
                guahaoMasterDTO.setGoodAt(master.getGoodAt());
                guahaoMasterDTO.setAmNum(masterAmNum);
                guahaoMasterDTO.setPmNum(masterPmNum);
                guahaoMasterDTOList.add(guahaoMasterDTO);
            }*/
            JsonConfig config = new JsonConfig();
            config.setExcludes(new String[]{"hospitalPermissionSet", "master", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList", "answer", "praiseUserSet", "answerList", "question", "masterScoreAnswer", "answerImgSet", "hospital", "masterAnswerList"});
            return JSONArray.fromObject(guahaoMasterList, config);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public JSONObject findMasterGuahaoNum(Integer masterId, String guahaoDate) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf1.parse(guahaoDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            Date date11 = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            Date date22 = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 24);
            Date date33 = calendar.getTime();
            String strDate11 = sdf.format(date11);
            String strDate22 = sdf.format(date22);
            String strDate33 = sdf.format(date33);
            Integer masterAmNum = guaHaoRepository.sumMasterGuahaoOneDayNum(masterId, strDate11, strDate22);
            Integer masterPmNum = guaHaoRepository.sumMasterGuahaoOneDayNum(masterId, strDate22, strDate33);
            GuahaoMasterDTO guahaoMasterDTO = new GuahaoMasterDTO();
            guahaoMasterDTO.setAmNum(masterAmNum);
            guahaoMasterDTO.setPmNum(masterPmNum);
            return JSONObject.fromObject(guahaoMasterDTO);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public JSONObject findGuahao(Integer hospitalId, Integer departmentId, String guahaoDate) {
        return null;
    }

    @Override
    public JSONArray findGuahaoMasterForSys(Integer hospitalId, Integer departmentId, String guahaoDate) {
        List<Guahao> guahaoList = guaHaoRepository.findAll(new Specification<Guahao>() {
            @Override
            public Predicate toPredicate(Root<Guahao> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //按时间搜索
                if (guahaoDate != null && !guahaoDate.equals("")) {
                    try {
                        Date date = sdf1.parse(guahaoDate);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        Date date11 = calendar.getTime();
                        calendar.set(Calendar.HOUR_OF_DAY, 12);
                        Date date22 = calendar.getTime();
                        calendar.set(Calendar.HOUR_OF_DAY, 24);
                        Date date33 = calendar.getTime();
                        String strDate11 = sdf.format(date11);
                        String strDate22 = sdf.format(date22);
                        String strDate33 = sdf.format(date33);
                        Predicate p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("ghDate").as(String.class), strDate11);
                        Predicate p2 = criteriaBuilder.lessThanOrEqualTo(root.get("ghDate").as(String.class), strDate33);
                        predicateList.add(p1);
                        predicateList.add(p2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }
                //只查询后七天以内的数据
                Date date = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.add(Calendar.DAY_OF_MONTH, +1);
                Date date11 = calendar.getTime();
                calendar.setTime(date);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.HOUR_OF_DAY, 24);
                calendar.add(Calendar.DAY_OF_MONTH, +7);
                Date date22 = calendar.getTime();

                String strDate11 = sdf.format(date11);
                String strDate22 = sdf.format(date22);

                Predicate p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("ghDate").as(String.class), strDate11);
                Predicate p2 = criteriaBuilder.lessThanOrEqualTo(root.get("ghDate").as(String.class), strDate22);
                predicateList.add(p1);
                predicateList.add(p2);
                //按部门搜索
                if (departmentId != null && !departmentId.equals("")) {
                    Predicate p = criteriaBuilder.equal(root.get("hospitalDepartmentId").as(Integer.class), departmentId);
                    predicateList.add(p);
                }
                //只查询自己机构的
                List<HospitalDepartment> hospitalDepartmentList = hospitalDepartmentRepository.findByHospitalId(hospitalId);
                List<Guahao> guahaoList1 = new ArrayList<>();
                for (HospitalDepartment department : hospitalDepartmentList) {
                    List<Guahao> guahaoList2 = guaHaoRepository.findByHospitalDepartmentId(department.getId());
                    for (Guahao guahao : guahaoList2) {
                        guahaoList1.add(guahao);
                    }
                }

                Predicate predicate = root.in(guahaoList1);
                predicateList.add(predicate);
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;

            }
        });
        List<SysGuahaoDTO> sysGuahaoDTOList = new ArrayList<>();
        for (Guahao guahao : guahaoList) {
            SysGuahaoDTO sysGuahaoDTO = new SysGuahaoDTO();
            sysGuahaoDTO.setGuahaoId(guahao.getId());
            sysGuahaoDTO.setMasterId(guahao.getMasterId());
            sysGuahaoDTO.setMasterName(masterRepository.findOne(guahao.getMasterId()).getName());
            sysGuahaoDTO.setDepartmentName(hospitalDepartmentRepository.findOne(guahao.getHospitalDepartmentId()).getDepartmentName());
            sysGuahaoDTO.setGuahaoDate(guahao.getGhDate());
            sysGuahaoDTO.setGuahaoDateTime(guahao.getDayTime());
            sysGuahaoDTO.setGuahaoNum(guahao.getGuahaoNum());
            sysGuahaoDTOList.add(sysGuahaoDTO);
        }

        return JSONArray.fromObject(sysGuahaoDTOList);

    }

    @Transactional
    @Override
    public void addGuahao(Integer guahaoDepartment, String addGuahaoDate, String addGuahaoDateTime, Integer addGuahaoNum, Integer masterSelect) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Guahao guahao1 = guaHaoRepository.findByGhDateAndMasterId(addGuahaoDate, masterSelect);
            if (guahao1 == null) {
                Guahao guahao = new Guahao();
                guahao.setMasterId(masterSelect);
                guahao.setHospitalDepartmentId(guahaoDepartment);
                guahao.setGuahaoNum(addGuahaoNum);

                Date date = sdf1.parse(addGuahaoDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                if ("AM".equals(addGuahaoDateTime)) {
                    guahao.setDayTime("上午");
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.HOUR_OF_DAY, 11);
                } else {
                    guahao.setDayTime("下午");
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.HOUR_OF_DAY, 16);
                }
                Date date11 = calendar.getTime();
                String strDate11 = sdf.format(date11);
                guahao.setGhDate(strDate11);
                guaHaoRepository.save(guahao);
            } else {
                guahao1.setGuahaoNum(addGuahaoNum);
                guaHaoRepository.save(guahao1);
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Transactional
    @Override
    public void updateGuahaoNum(Integer guahaoId, Integer guahaoNum) {
        Guahao guahao = guaHaoRepository.findOne(guahaoId);
        guahao.setGuahaoNum(guahaoNum);
        guaHaoRepository.save(guahao);

    }

    @Override
    public void addSevenDayGuahao() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 11);
        calendar.add(Calendar.DAY_OF_MONTH, +7);
        String date1 = sdf.format(calendar.getTime());
        List<Master> masterList = masterRepository.findAll();
        for (Master master : masterList) {
            Guahao guahao = new Guahao();
            guahao.setGhDate(date1);
            guahao.setGuahaoNum(0);
            guahao.setDayTime("上午");
            guahao.setMasterId(master.getId());
            guahao.setHospitalDepartmentId(master.getHospitalDepartmentId());
            guaHaoRepository.save(guahao);
        }
        //下午
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        String date2 = sdf.format(calendar.getTime());
        for (Master master : masterList) {
            Guahao guahao = new Guahao();
            guahao.setGhDate(date2);
            guahao.setGuahaoNum(0);
            guahao.setMasterId(master.getId());
            guahao.setDayTime("下午");
            guahao.setHospitalDepartmentId(master.getHospitalDepartmentId());
            guaHaoRepository.save(guahao);
        }
    }
}
